package com.sk.sourcecircle.activity.home.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.login.LoginActivity;
import com.sk.sourcecircle.activity.mine.act.PreviewViewpagerActivity;
import com.sk.sourcecircle.adapter.CommentAdapter;
import com.sk.sourcecircle.adapter.PictureAdapter;
import com.sk.sourcecircle.adapter.PraiseDetailsAdapter;
import com.sk.sourcecircle.base.BaseActivity;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.entity.AlbumaBean;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.MyUtils;
import com.sk.sourcecircle.utils.SnackUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2017/12/28.
 */

public class AblumDetailsActivity extends BaseActivity implements HandleDataCallBack {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.my_head)
    ImageView myHead;
    @BindView(R.id.my_name)
    TextView myName;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.from_group)
    TextView fromGroup;
    @BindView(R.id.add_care)
    TextView addCare;
    @BindView(R.id.cancel_care)
    TextView cancelCare;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.grid_view)
    RecyclerView gridView;
    @BindView(R.id.praise)
    ImageView praise;
    @BindView(R.id.praise_ryc)
    RecyclerView praiseRyc;
    @BindView(R.id.comment_ryc)
    RecyclerView commentRyc;
    @BindView(R.id.cancel_praise)
    ImageView cancelPraise;
    @BindView(R.id.ed_comment)
    EditText edComment;
    @BindView(R.id.add_collect)
    ImageView addCollect;
    @BindView(R.id.cancel_collect)
    ImageView cancelCollect;

    private AlbumaBean dataBean;
    List<AlbumaBean.DetailBean.ReplyBean> replyList = new ArrayList<>();
    List<AlbumaBean.DetailBean.ReplyBean> praiseList = new ArrayList<>();
    private PraiseDetailsAdapter praiseDetailsAdapter = null;
    private CommentAdapter commentAdapter = null;
    private LoadingDialog loadingDialog;
    private String comment = "";

    @Override
    protected int getContentView() {
        return R.layout.activity_ablum_details_activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        praiseRyc.setLayoutManager(new GridLayoutManager(mContext, 7));
        praiseDetailsAdapter = new PraiseDetailsAdapter(R.layout.item_prasie_pic, praiseList);
        praiseRyc.setAdapter(praiseDetailsAdapter);
        praiseDetailsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext, PersonDetailsActivity.class);
                intent.putExtra("id", praiseList.get(position).getUser_id());
                startActivity(intent);
            }
        });


        commentRyc.setLayoutManager(new LinearLayoutManager(mContext));
        commentAdapter = new CommentAdapter(R.layout.item_details_comment, replyList);
        commentRyc.setAdapter(commentAdapter);


        loadingDialog = MyUtils.getLoadingDialog(mContext, false);

        dataBean = getIntent().getParcelableExtra("data");

        fromGroup.setText(dataBean.getDetail().getCommunity_name());
        tvContent.setText(dataBean.getRank());

        edComment.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    // 先隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    //进行搜索操作的方法，在该方法中可以加入mEditSearchUser的非空判断
                    if (edComment.getText().toString().length() > 0) {
                        loadingDialog.show();
                        comment = edComment.getText().toString();
                        AppApi.commentAlbum(dataBean.getAlbum_id(), edComment.getText().toString(), AblumDetailsActivity.this, 1, AblumDetailsActivity.this);
                    } else {
                        SnackUtils.showError(mRootView,"发送的消息不能为空哦~");
                    }
                }
                return false;
            }
        });

        Glide.with(mContext).load(dataBean.getDetail().getHead_url()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(myHead);
        myName.setText(dataBean.getDetail().getNickname());
        tvTime.setText(MyUtils.getTimeFormatText(dataBean.getCreate_time()) + "   来自");
        if (dataBean.getDetail().getIs_friend() == 1) {
            cancelCare.setVisibility(View.VISIBLE);
            addCare.setVisibility(View.GONE);
        } else {
            cancelCare.setVisibility(View.GONE);
            addCare.setVisibility(View.VISIBLE);
        }

        if (dataBean.getDetail().getIs_dig() == 1) {
            cancelPraise.setVisibility(View.VISIBLE);
            praise.setVisibility(View.INVISIBLE);
        } else {
            cancelPraise.setVisibility(View.INVISIBLE);
            praise.setVisibility(View.VISIBLE);
        }

        if (dataBean.getDetail().getIs_favorite() == 1) {
            cancelCollect.setVisibility(View.VISIBLE);
            addCollect.setVisibility(View.INVISIBLE);
        } else {
            cancelCollect.setVisibility(View.INVISIBLE);
            addCollect.setVisibility(View.VISIBLE);
        }

        List<AlbumaBean.DetailBean.ReplyBean> replyBeanList = dataBean.getDetail().getReply();


        if (replyBeanList != null && replyBeanList.size() > 0) {


            for (AlbumaBean.DetailBean.ReplyBean data : replyBeanList) {
                if (data.getType().equals("reply")) {
                    replyList.add(data);
                } else if (data.getType().equals("dig")) {
                    praiseList.add(data);
                }
            }

            commentAdapter.notifyDataSetChanged();
            praiseDetailsAdapter.notifyDataSetChanged();


            commentRyc.setHasFixedSize(true);
            commentRyc.setNestedScrollingEnabled(false);
        }


        gridView.setLayoutManager(new GridLayoutManager(mContext, 3));
        final List<String> stringList = dataBean.getImages();
        PictureAdapter adapter = new PictureAdapter(R.layout.item_ablum_picture, stringList);
        gridView.setAdapter(adapter);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext, PreviewViewpagerActivity.class);
                Activity activity = (Activity) mContext;
                intent.putExtra("pos", position);
                intent.putStringArrayListExtra("list", (ArrayList<String>) stringList);
                activity.startActivity(intent);
            }
        });
    }

    @OnClick({R.id.back, R.id.my_head, R.id.add_care, R.id.cancel_care, R.id.praise, R.id.cancel_praise, R.id.add_collect, R.id.cancel_collect})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.my_head:
                Intent intent = new Intent(mContext, PersonDetailsActivity.class);
                intent.putExtra("id", dataBean.getUser_id());
                startActivity(intent);
                break;
            case R.id.add_care:
                if (MyUtils.getToken(mContext) != null && MyUtils.getToken(mContext).length() == 0) {
                    Toast.makeText(mContext, "您尚未登陆，请先登录！", Toast.LENGTH_SHORT).show();
                    Intent loginIntent = new Intent(mContext, LoginActivity.class);
                    startActivityForResult(loginIntent, 100);
                    return;
                }
                loadingDialog.show();
                AppApi.addFriend(dataBean.getUser_id(), AblumDetailsActivity.this, 4, AblumDetailsActivity.this);
                break;
            case R.id.cancel_care:
                break;
            case R.id.praise:
                if (MyUtils.getToken(mContext) != null && MyUtils.getToken(mContext).length() == 0) {
                    Toast.makeText(mContext, "您尚未登陆，请先登录！", Toast.LENGTH_SHORT).show();
                    Intent loginIntent = new Intent(mContext, LoginActivity.class);
                    startActivityForResult(loginIntent, 100);
                    return;
                }
                loadingDialog.show();
                AppApi.praiseAlbum(dataBean.getAlbum_id(), AblumDetailsActivity.this, 2, AblumDetailsActivity.this);
                break;
            case R.id.cancel_praise:
                break;
            case R.id.add_collect:
                if (MyUtils.getToken(mContext) != null && MyUtils.getToken(mContext).length() == 0) {
                    Toast.makeText(mContext, "您尚未登陆，请先登录！", Toast.LENGTH_SHORT).show();
                    Intent loginIntent = new Intent(mContext, LoginActivity.class);
                    startActivityForResult(loginIntent, 100);
                    return;
                }
                loadingDialog.show();
                AppApi.collectAlbum(dataBean.getAlbum_id(), AblumDetailsActivity.this, 6, AblumDetailsActivity.this);
                break;
            case R.id.cancel_collect:
                break;
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        switch (tag) {
            case 1:
                SnackUtils.show(mRootView,"评论发布成功了o~");
                edComment.setText("");
                AlbumaBean.DetailBean.ReplyBean replyBean = new AlbumaBean.DetailBean.ReplyBean();
                replyBean.setContent(comment);
                replyBean.setType("reply");
                DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                replyBean.setCreate_time(fmt.format(curDate));
                replyBean.setHead_url(MyUtils.getMyInfo(mContext).getHead_url());
                replyBean.setNickname(MyUtils.getUser(mContext).getNickname());
                replyList.add(replyBean);
                commentAdapter.notifyDataSetChanged();
                break;
            case 6:
                SnackUtils.show(mRootView,"相册收藏成功le~");
                cancelCollect.setVisibility(View.VISIBLE);
                addCollect.setVisibility(View.INVISIBLE);
                break;
            case 2:
                SnackUtils.show(mRootView,"点赞成功le o~");
                cancelPraise.setVisibility(View.VISIBLE);
                praise.setVisibility(View.INVISIBLE);
                AlbumaBean.DetailBean.ReplyBean praiseBean = new AlbumaBean.DetailBean.ReplyBean();
                praiseBean.setType("dig");
                praiseBean.setUser_id(MyUtils.getCode(mContext));
                praiseBean.setHead_url(MyUtils.getMyInfo(mContext).getHead_url());
                praiseBean.setNickname(MyUtils.getUser(mContext).getNickname());
                praiseList.add(praiseBean);
                praiseDetailsAdapter.notifyDataSetChanged();
                break;
            case 4:
                SnackUtils.show(mRootView,"请求发送成功，请等待对方回复o~");
                break;
        }
    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();

    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

}
